﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyTrader.Chart
{
    public partial class CmeDailyChartFrm : System.Windows.Forms.Form
    {
        public CmeDailyChartFrm()
        {
            InitializeComponent();
            CmeDailyChartCtrl.Series["지수"].ToolTip = "#VALX : #VAL";
        }

        int curChartType = 0;
        // Cybos Plus Data Set Member
        private ETDataSet m_CPDataSet = null;
        public EasyTrader.ETDataSet CPDataSet
        {
            get { return m_CPDataSet; }
            set { m_CPDataSet = value; }
        }
        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        private void InitTimer()
        {
            timer.Interval = 1000;
            timer.Tick += new EventHandler(OnTimerTick);
            timer.Start();
        }

        // 여기서 차트를 갱신해 준다.
        private void OnTimerTick(object sender, EventArgs e)
        {
            // 차트에 데이터를 대응시켜 준다.
            SetChartData();
        }

        private void SetChartData()
        {
            // 데이터 셋 객체가 없으면 아무일도 하지 않는다.
            if (m_CPDataSet == null || GlobalVar.CmeDailyDataDownloaded == false)
                return;

            CmeDailyChartCtrl.Series["지수"].Points.Clear();
            EasyTrader.DataSet.CmeDailyTable dataTable = m_CPDataSet.GetCmeDailyTable();
            double maxValue = -10000.0;
            double minValue = 10000.0;
            int rowCount = dataTable.GetRowCount();
            int minIndex = 0;
            int maxIndex = 0;
            double highValue = 0;
            double lowValue = 0;
            double openValue = 0;
            double closeValue = 0;
            for (int i = 0; i < rowCount; i++)
            {
                DataRow row = dataTable.Rows[i];
                try
                {
                    // 값이 없을 경우는 넘어 간다.
                    if (row["고가"] == DBNull.Value)
                        continue;

                    highValue = Convert.ToDouble(row["CME고가"]);
                    lowValue = Convert.ToDouble(row["CME저가"]);
                    openValue = Convert.ToDouble(row["CME시가"]);
                    closeValue = Convert.ToDouble(row["CME종가"]);
                    if (curChartType == 0)
                    {
                        CmeDailyChartCtrl.Series["지수"].Points.AddXY(i, highValue);
                        CmeDailyChartCtrl.Series["지수"].Points[i].XValue = i;
                        CmeDailyChartCtrl.Series["지수"].Points[i].AxisLabel = row["날짜"].ToString();
                        CmeDailyChartCtrl.Series["지수"].Points[i].YValues[1] = lowValue;
                        CmeDailyChartCtrl.Series["지수"].Points[i].YValues[2] = openValue;
                        CmeDailyChartCtrl.Series["지수"].Points[i].YValues[3] = closeValue;
                    }
                    else
                    {
                        CmeDailyChartCtrl.Series["지수"].Points.AddXY(i, closeValue);
                        CmeDailyChartCtrl.Series["지수"].Points[i].XValue = i;
                        CmeDailyChartCtrl.Series["지수"].Points[i].AxisLabel = row["날짜"].ToString();
                        Console.WriteLine(CmeDailyChartCtrl.Series["지수"].Points.Count.ToString());
                    }

                    if (highValue != 0 && highValue > maxValue)
                    {
                        maxValue = highValue;
                        maxIndex = i;
                    }

                    if (lowValue != 0 && lowValue > maxValue)
                    {
                        maxValue = lowValue;
                        maxIndex = i;
                    }

                    if (openValue != 0 && openValue > maxValue)
                    {
                        maxValue = openValue;
                        maxIndex = i;
                    }

                    if (closeValue != 0 && closeValue > maxValue)
                    {
                        maxValue = closeValue;
                        maxIndex = i;
                    }

                    if (highValue != 0 && highValue < minValue)
                    {
                        minValue = highValue;
                        minIndex = i;
                    }

                    if (lowValue != 0 && lowValue < minValue)
                    {
                        minValue = lowValue;
                        minIndex = i;
                    }

                    if (openValue != 0 && openValue < minValue)
                    {
                        minValue = openValue;
                        minIndex = i;
                    }

                    if (closeValue != 0 && closeValue < minValue)
                    {
                        minValue = closeValue;
                        minIndex = i;
                    }
                    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            double valueHeight = maxValue - minValue;
            double ceilVal = Math.Ceiling(valueHeight);
            maxValue = minValue + ceilVal;

            if (maxValue >= minValue)
            {
                CmeDailyChartCtrl.ChartAreas["Price"].AxisY.Minimum = minValue;
                CmeDailyChartCtrl.ChartAreas["Price"].AxisY.Maximum = maxValue;
            }
            timer.Stop();
        }

        public CmeDailyChartFrm(ETDataSet a_DataSet)
        {
            InitializeComponent();
            m_CPDataSet = a_DataSet;
            CmeDailyChartCtrl.Series["지수"].ToolTip = "#VALX : #VAL";
            comboChartType.SelectedIndex = GlobalVar.CmeDailyChartType;
        }

        private void InitChart()
        {
            CmeDailyChartCtrl.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(211)), ((System.Byte)(223)), ((System.Byte)(240)));
            CmeDailyChartCtrl.BackSecondaryColor = System.Drawing.Color.White;
            CmeDailyChartCtrl.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            CmeDailyChartCtrl.BorderlineColor = System.Drawing.Color.FromArgb(((System.Byte)(26)), ((System.Byte)(59)), ((System.Byte)(105)));
            CmeDailyChartCtrl.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            CmeDailyChartCtrl.BorderlineWidth = 2;
            CmeDailyChartCtrl.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;

            CmeDailyChartCtrl.ChartAreas["Price"].AxisX.MajorGrid.Interval = 10;
            CmeDailyChartCtrl.ChartAreas["Price"].AxisX.Interval = 10;
            CmeDailyChartCtrl.ChartAreas["Price"].AxisY.MajorGrid.Interval = 1;
            CmeDailyChartCtrl.ChartAreas["Price"].AxisY.Interval = 1;

            CmeDailyChartCtrl.ChartAreas["Price"].Area3DStyle.IsClustered = true;
            CmeDailyChartCtrl.ChartAreas["Price"].Area3DStyle.Perspective = 10;
            CmeDailyChartCtrl.ChartAreas["Price"].Area3DStyle.IsRightAngleAxes = false;
            CmeDailyChartCtrl.ChartAreas["Price"].Area3DStyle.WallWidth = 0;
            CmeDailyChartCtrl.ChartAreas["Price"].Area3DStyle.Inclination = 15;
            CmeDailyChartCtrl.ChartAreas["Price"].Area3DStyle.Rotation = 10;
            CmeDailyChartCtrl.ChartAreas["Price"].AxisX.IsLabelAutoFit = true;
            CmeDailyChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            CmeDailyChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.IsEndLabelVisible = false;
            CmeDailyChartCtrl.ChartAreas["Price"].AxisX.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            CmeDailyChartCtrl.ChartAreas["Price"].AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            CmeDailyChartCtrl.ChartAreas["Price"].AxisY.IsLabelAutoFit = true;
            CmeDailyChartCtrl.ChartAreas["Price"].AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            CmeDailyChartCtrl.ChartAreas["Price"].AxisY.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            CmeDailyChartCtrl.ChartAreas["Price"].AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            CmeDailyChartCtrl.ChartAreas["Price"].AxisY.IsStartedFromZero = false;
            //CmeDailyChartCtrl.ChartAreas["Price"].AxisY.LabelAutoFitMaxFontSize;
            CmeDailyChartCtrl.ChartAreas["Price"].BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
            CmeDailyChartCtrl.ChartAreas["Price"].BackSecondaryColor = System.Drawing.Color.White;
            CmeDailyChartCtrl.ChartAreas["Price"].BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            CmeDailyChartCtrl.ChartAreas["Price"].BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            CmeDailyChartCtrl.ChartAreas["Price"].BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            CmeDailyChartCtrl.ChartAreas["Price"].Position.Auto = true;
            //CmeDailyChartCtrl.ChartAreas["Price"].Position.Height = 80F;
            //CmeDailyChartCtrl.ChartAreas["Price"].Position.Width = 80F;
            //CmeDailyChartCtrl.ChartAreas["Price"].Position.X = 5F;
            //CmeDailyChartCtrl.ChartAreas["Price"].Position.Y = 10F;
            CmeDailyChartCtrl.ChartAreas["Price"].ShadowColor = System.Drawing.Color.Transparent;
            if (GlobalVar.CmeDailyChartType == 0)
            {
                CmeDailyChartCtrl.Series["지수"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Candlestick;
                comboChartType.SelectedIndex = 0;
            }
            else
            {
                CmeDailyChartCtrl.Series["지수"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                comboChartType.SelectedIndex = 1;
            }

            CmeDailyChartCtrl.Series[0]["PriceUpColor"] = "Red";
            CmeDailyChartCtrl.Series[0]["PriceDownColor"] = "Blue";
            CmeDailyChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.IsEndLabelVisible = true;
            CmeDailyChartCtrl.ChartAreas["Price"].AxisX.IsMarginVisible = true;
            CmeDailyChartCtrl.ChartAreas["Price"].AxisY.LabelStyle.IsEndLabelVisible = true;
            CmeDailyChartCtrl.ChartAreas["Price"].AxisY.IsMarginVisible = true;
            //CmeDailyChartCtrl.Series[0].Color = Color.Red;
            //DataPointCustomProperties aa = CmeDailyChartCtrl.Series[0]["PriceUpColor"];
        }

        private void CmeDailyChartFrm_Load(object sender, EventArgs e)
        {
            InitChart();
            InitTimer();
            curChartType = GlobalVar.CmeDailyChartType;
            GlobalVar.CmeDailyRefCount++;
        }

        private void CmeDailyChartFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer.Stop();
            GlobalVar.CmeDailyRefCount--;
        }

        private void comboCmeDailyChartType_SelectedIndexChanged(object sender, EventArgs e)
        {
            curChartType = comboChartType.SelectedIndex;
            if (curChartType != -1)
            {
                if (curChartType == 0)
                {
                    CmeDailyChartCtrl.Series["지수"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Candlestick;
                }
                else
                {
                    CmeDailyChartCtrl.Series["지수"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                }
                SetChartData();
                CmeDailyChartCtrl.Invalidate();
            }
        }
    }
}
